#include "../include/mysqlu.h"
#include"../include/my_head.h"
#include"../include/log.h"


int con_mysql(MYSQL *mysql)
{
    //这里的ret作用是捕捉连接是否成功
    MYSQL * ret = mysql_real_connect(mysql,
                            "192.168.7.22",
                            "root",
                            "1",
                            "zhaoTest",
                            3306,NULL,0);
    if(ret == NULL){
        fprintf(stderr,"%s:%s\n","mysql_real_connect",mysql_error(mysql));
        return -1;
    }
    // mysql_close(mysql);
    return 0;
}

int mysql_insert_user_mysql(MYSQL *mysql,user_t *user)
{   
    printf("--------------------------**-----------------------\n");
    printf("user->username=%s,user->salt=%s,user->hashed_password=%s,user->tomb=%d\n", 
    user->username,user->salt,user->hashed_password,user->tomb);
    char sql[1024];
    char salt_s[17];
    char kuf[64];
    bzero(kuf,sizeof(kuf));
    bzero(salt_s,sizeof(salt_s));
    strncpy(salt_s, user->salt, 16);  // salt_s大小为16字节
    salt_s[sizeof(salt_s)-1] = '$';   // 尝试在第15位（索引15）添加'$'
    salt_s[sizeof(salt_s)] = '\0';    // 错误：数组越界（最大索引为15）
    strncpy(kuf,user->hashed_password,64); 
    kuf[63] = '\0'; 
    printf("--------------------------**-----------------------\n");
    printf("salt==%s\n",salt_s);
    printf("pass=%s\n",kuf);
    printf("--------------------------**-----------------------\n");

    if(user!=NULL){
        bzero(sql,sizeof(sql));
        // insert into super_star(name, salt,hash_password,tomb) values('%s','%s');",name,works;
        
        snprintf(sql,sizeof(sql),"insert into user(username, salt,hashed_password,tomb)values('%s','%s','%s','%d');",
                user->username,salt_s,kuf,user->tomb);
        printf("%s\n",sql);
        int qret=mysql_query(mysql,sql);
        if(qret != 0){
            fprintf(stderr,"%s:%s\n","mysql_query usertable",mysql_error(mysql));
            return -1;
        }
        return 0;
    }
    return -2;    
}

int mysql_insert_file_mysql(MYSQL *mysql,file_t *file)
{
     char sql[1024];
    if(file!=NULL){
        bzero(sql,sizeof(sql));
        // insert into super_star(name, salt,hash_password,tomb) values('%s','%s');",name,works;
        snprintf(sql,sizeof(sql),"insert into file(username, parent_id,filename,filetype,hashfilename,path,tomb)values\
        ('%s','%d','%s','%c','%s','%s','%d');",
        file->username,file->parent_id,file->filename,file->filetype,file->hashfilename,file->path,file->tombs);
        int qret=mysql_query(mysql,sql);
        if(qret != 0){
            fprintf(stderr,"%s:%s\n","mysql_query",mysql_error(mysql));
            return -1;
        }
        return 0;  
    }
    return -2;
}

int select_mysql_user(MYSQL *mysql,user_t *user,char *username)
{
    char sql[1024];
    if(username!=NULL){
        bzero(sql,sizeof(sql));
      
        snprintf(sql,sizeof(sql),"select * from user where username='%s';",username);
        printf("%s\n",sql);
        int qret=mysql_query(mysql,sql);
        if(qret != 0){
            fprintf(stderr,"%s:%s\n","mysql_query",mysql_error(mysql));
            return -1;
        }
        MYSQL_RES *table = mysql_store_result(mysql);//取出一张表
        int cols = mysql_num_fields(table);//列数
        long rows = mysql_num_rows(table);//行数
        printf("usernam%s\n, cols = %d, rows = %ld", username,mysql_num_fields(table), mysql_num_rows(table));
        for(long i = 0; i < rows; ++i){
            MYSQL_ROW row = mysql_fetch_row(table);//取出下一行
            int j=0;
            strcpy(user->username,row[1]);
            strcpy(user->salt,row[2]);
            strncpy(user->hashed_password,row[3],64);
        }
        user->hashed_password[63]='\0';
       printf("username:%s,nsalt:%s,nhash:%s\ntomb%d\n",user->username,user->salt,user->hashed_password,user->tomb);
    }
     return 0;
}


